﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;

namespace cmdrunner
{
    internal class WMIUtil
    {
        public static ManagementObjectCollection Execute(String Script)
        {
            ManagementScope scope = new ManagementScope("\\\\localhost\\root\\cimv2");
            SelectQuery query = new SelectQuery(Script);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection queryCollection = searcher.Get();

            return queryCollection;
        }

        public static String ExecuteFmt(String Script, params String[] Cols)
        {
            ManagementObjectCollection list = Execute(Script);
            StringBuilder sb = new StringBuilder();

            foreach (String col in Cols)
            {
                sb.Append(col).Append("\t");
            }

            foreach (ManagementObject o in list)
            {
                foreach (String col in Cols)
                {
                    sb.Append(Convert.ToString(o[col])).Append("\t");
                }
                sb.AppendLine();
            }

            return sb.ToString();

        }

    }
}
